<!DOCTYPE HTML>
<html>
<head>
	<title>我的个人博客</title>
	<meta charset="utf8">
	<link rel="stylesheet" type="text/css" href="style.css">
	<link rel="icon"  type="image/icon" href="simbs.png">
</head>
<body>

<div class="top">
	<div class="header"> 
                <div class="logo">
                        <a href="index.php"><img src="logo.png"/></a>
                </div>
		<div id="container">
			<ul>
<?php
	//调用配置文件config.php
	require_once 'config.php';
	//连接数据库 
	$conn = mysql_connect($host, $dbusername,$dbpassword);
	if(!$conn){
		die("连接失败".mysql_error());
	}
	//设置访问数据库的编码
	mysql_query("set names utf8",$conn) or die(mysql_errno());
	//选择数据库
	mysql_select_db($dbname,$conn) or die(mysql_errno());

	
	$sql = "select * from catalogs";
	$res = mysql_query($sql,$conn);
	while($row=mysql_fetch_assoc($res)){
		$cid = $row['cid'];
		echo "<li><a href=blogManage.php?list=$cid>{$row['catalog']}</a></li>";
	}
	mysql_free_result($res);
	mysql_close($conn);
?>
			</ul>
		</div>
	</div>
	<div class="single">
		<div class="content">

<?php	
	//设置页码
	if(!empty($_GET['pageNow'])){
		$pageNow=$_GET['pageNow'];
	}else{
		$pageNow=1;
	}

	//调用配置文件config.php
	require_once 'config.php';
	//连接数据库 
	$conn = mysql_connect($host, $dbusername,$dbpassword);
	if(!$conn){
		die("连接失败".mysql_error());
	}
	//设置访问数据库的编码
	mysql_query("set names utf8",$conn) or die(mysql_errno());
	//选择数据库
	mysql_select_db($dbname,$conn) or die(mysql_errno());


	//如果接受到$_GET['flag']='delete',则删掉那个id的文章
	if(!empty($_GET['flag'])) {
		$id = $_GET['id'];
		$sql = "delete from blog where id=$id";
		$res = mysql_query($sql, $conn) ;
	}

	//输出记录行数$rowCount
	$sql = "select count(id) from blog";
	$result = mysql_query($sql);
	if ($row = mysql_fetch_row($result)){
		$rowCount = $row[0];
	}
	
	//设置每页显示两条记录$pageSize
	$pageSize=10;
	
	//计算页数$pageCount
	$pageCount = ceil($rowCount/$pageSize);		

	//发送sql查询语句
	//！limit后面必须要有空格
	$sql = "select * from blog order by time desc limit ".($pageNow-1)*$pageSize.",$pageSize";
	$res = mysql_query($sql, $conn);
	echo "<table width='550px' border='1px' bordercolor='green' cellspacing='0'>";
	echo "<tr><td>id</td><td>标题</td><td>时间</td><td>操作</td></tr>";
	
	//循环显示用户信息
	while($row = mysql_fetch_assoc($res)){
	$id = $row['id'];
	echo "<tr>
		<td>$id</td>
		<td><a href='blogPage.php?id=$id'>{$row['title']}</a></td>
		<td>{$row['time']}</td>
		<td><a href='blogList.php?flag=delete&id=$id'>删除</a></td></tr>";
	}
	echo "</table>";

	//关闭资源，关闭数据库
	mysql_free_result($res);
	mysql_close($conn);

	//打印页码的超链接
/*	for($i=1;$i<=$pageCount;$i++){
		echo "<a href='blogList.php?pageNow=$i'>$i&nbsp;</a>";
	}
*/	
	//上面的打印页码超链接的方式，如果没限制，记录太多会使访问变慢
	//显示上一页和下一页
	 if($pageNow>1){
		$prePage=$pageNow-1;
		echo "<a href='blogList.php?pageNow=$prePage'>上一页</a>";
	}
	if($pageNow<$pageCount){
		$nextPage=$pageNow+1;
		echo "<a href='blogList.php?pageNow=$nextPage'>下一页</a>";
	}	
?>

	<form action="blogList.php" >
	<span>
<?php
	//打印当前页码和总页数
	echo  "&nbsp;当前第".$pageNow."页"."&nbsp;总共".$pageCount."页";
	//可以用: echo "&nbsp;当前第{$pageNow}页&nbsp;总共{$pageCount}页";


?>
	<!--跳转到指定页-->
		转到第<input type="text" name="pageNow" style="width:20px;"/>页
		<input type="submit" value="Go"/>
	</span>
	</form>
		</div>
	</div>
	<div class="sidebar">
		<div class="loginSuccess">
			<?php
				session_start();
				if(!empty($_SESSION['name'])){
				echo "当前用户: ".$_SESSION['name']."<br />";
                                echo "<a href='blogList.php'>管理博文</a><br/>";
                                echo "<a href='blogAdd.php'>添加博文</a><br/>";
                                echo "<a href='configManage.php'>管理设置</a><br/>";
				echo "<a href='login.php'>重新登录</a>";
				} else{
				echo "<a href='login.php'>用户登录</a>";
				}
                	?>      

		</div>
                <div class="latest">最近更新
                <ul>
<?php
	//调用配置文件config.php
	require_once 'config.php';
	//连接数据库 
	$conn = mysql_connect($host, $dbusername,$dbpassword);
	if(!$conn){
		die("连接失败".mysql_error());
	}
	//设置访问数据库的编码
	mysql_query("set names utf8",$conn) or die(mysql_errno());
	//选择数据库
	mysql_select_db($dbname,$conn) or die(mysql_errno());



        //查询最新的10条记录
        $sql = "select title,id from blog order by time desc limit 0,9";
        $res = mysql_query($sql,$conn);

        //打印结果
        while($row=mysql_fetch_assoc($res)){
		$id = $row['id'];
                echo "<li><a href='blogPage.php?id=$id'>{$row['title']}</a></li>";
        }

        //关闭资源和数据库
        mysql_free_result($res);
        mysql_close($conn);
?>
                </ul>
        	</div>
	</div>
	<div id="footer">
		<p>Copyright &#169;2012 Powered by <a href="http://code.google.com/p/simbs/">simbs</a> </p>
	</div>
</div>
</body>
</html>
